import Vue from 'vue'
import API from '@/api'
import Http from '@/http'
import DateUtil from '@/utils/dateutil'
import Store from '@/store'
import _ from 'underscore'
/**
 * 从基础数据显示 data_text
 */
Vue.filter('show_basedata', (data_value,data_type,tip='') => {
  let basedata = Store.getters.basedata
  if(basedata.empty_flag){
    basedata = Http.syncRequest(API.get_basedata).data
    Store.commit("SET_BASEDATA",basedata)
  }
  let obj = _.find(basedata[data_type],function (item) {
      return item.data_value===data_value;
  })
  if(obj){
    return obj.data_text
  }else{
    return tip
  }
});
Vue.filter('show_basedatas', (data_value,data_type,tip='') => {
  let basedata = Store.getters.basedata
  if(basedata.empty_flag){
    basedata = Http.syncRequest(API.get_basedata).data
    Store.commit("SET_BASEDATA",basedata)
  }
  if(data_value){
    let obj_list = _.filter(basedata[data_type],function (item) {
      return _.indexOf(data_value.split(","),item.data_value)>=0
    })
    if(obj_list&&obj_list.length>0){
      return _.pluck(obj_list,'data_text').join(",")
    }else{
      return tip
    }
  }else{
    return tip
  }
});

Vue.filter('sub_text', (str, len) => {
  if(str){
    if(str.length*2 <= len) {
      return str;
    }
    var strlen = 0;
    var s = "";
    for(var i = 0;i < str.length; i++) {
      s = s + str.charAt(i);
      if (str.charCodeAt(i) > 128) {
        strlen = strlen + 2;
        if(strlen >= len){
          return s.substring(0,s.length-1) + "...";
        }
      } else {
        strlen = strlen + 1;
        if(strlen >= len){
          return s.substring(0,s.length-2) + "...";
        }
      }
    }
    return s;
  }else{
    return "";
  }
});

Vue.filter('parseFloat', (value) => {
	if(value){
		return parseFloat(value).toFixed(2);
	}
});
Vue.filter('fixedTwo', (value) => {
	if(value){
		return parseFloat(value).toFixed(2);
	}
});
Vue.filter('formatZero', (value,tip) => {
	if(value==0){
		return tip;
	}else{
	    return value
    }
});
Vue.filter('tip', (value,tips) => {
  if(Util.trim(value)){
      return value;
  }else{
      return tips
  }
});
Vue.filter('sp', (value,a,b) => {
  if(value){
      var arr = value.split(a);
      return arr.join(b);
  }else{
      return ""
  }
});
Vue.filter('first_img', (value) => {
  if(value){
    var arr = value.split(",");
    return arr[0];
  }else{
    return ""
  }
});


Vue.filter('beautiful_time',function(str){
  var result= "---";
  if(str&&str!='0000-00-00 00:00:00'){
    var today= new Date();
    var date = DateUtil.strToDate(str);
    if(date.getDate()===today.getDate()){ //如果是 今天发的
      result= "今天 "+ DateUtil.dateToStr("HH:mm",date);
    }else if(date.getDate()===today.getDate()-1){ //如果是 昨天发的
      result= "昨天 "+ DateUtil.dateToStr("HH:mm",date);
    }else if(date.getDate()===today.getDate()-2){ //如果是 前天发的
      result= "前天 "+ DateUtil.dateToStr("HH:mm",date);
    }else{
      result= DateUtil.dateToStr("MM/dd HH:mm",date);
    }
  }
  return result;
})
Vue.filter('pure_city', (city) => {
  if(city){
    let arr = city.split('市')
    return arr[0]
  }else{
    return ''
  }
});

Vue.filter('mask_name', (name) => {
  if(name){
    return name.substr(0,1)+"**"
  }else{
    return ''
  }
});

Vue.filter('showTrainMun', (str) => {
  if(str){
    var train_obj = JSON.parse(str);
    if(train_obj.length){
      return train_obj.length + '级培训'
    }else{
      return '未配置'
    }
  }else{
  }
});

Vue.filter('showFixedAge', (birthday) => {
  if(!birthday){
    return "---"
  }
  if(birthday=='0000-00-00'){
    return "---"
  }
  var returnAge;
  var strBirthdayArr = birthday.split("-");
  var birthYear = strBirthdayArr[0];
  var birthMonth = strBirthdayArr[1];
  var birthDay = strBirthdayArr[2];

  var d = new Date();
  var nowYear = d.getFullYear();
  var nowMonth = d.getMonth() + 1;
  var nowDay = d.getDate();

  if (nowYear == birthYear) {
    returnAge = 0;//同年 则为0岁
  }
  else {
    var ageDiff = nowYear - birthYear; //年之差
    if (ageDiff > 0) {
      if (nowMonth == birthMonth) {
        var dayDiff = nowDay - birthDay;//日之差
        if (dayDiff < 0) {
          returnAge = ageDiff - 1;
        }
        else {
          returnAge = ageDiff;
        }
      }
      else {
        var monthDiff = nowMonth - birthMonth;//月之差
        if (monthDiff < 0) {
          returnAge = ageDiff - 1;
        }
        else {
          returnAge = ageDiff;
        }
      }
    }
    else {
      returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
    }
  }
  return returnAge;//返回周岁年龄
});

Vue.filter('fixedStatus', (str) => {
  if(str==-1){
    return "面试未通过"
  }else if(str==999){
    return "面试已通过"
  }else if(!str||str==0||(str=='null')){
    return "---"
  }else{
    return str+"面中"
  }
});

Vue.filter('translateAge', (str) => {
  if(str){
    return DateUtil.getAge(str);
  }
});

Vue.filter('decodeImg', (value) => {
  if(value){
    var value = value;
    if(!util.startWith("http",value)){
      if(value.indexOf(",")>-1){
        return value
      }else{
        return "data:image/jpeg;base64,"+value
      }
    }
    return value;
  }
  else{
    return false
  }
});

Vue.filter('getProvince', (address) => {
  var result = "";
  if(address){
    var arr1= address.split("省");
    if(arr1.length>1){
      result = arr1[0]+"省";
    }else{
      var arr2 = address.split("市");
      result = arr2[0]+"市";
    }
  }
  return result.substring(0,4);
});

Vue.filter('showFixedTime', (str) => {
  var result = "";
  if (str) {
    var today = new Date();
    var date = DateUtil.strToDate(str);

    if (date.getMonth() + "-" + date.getDate() == today.getMonth() + "-" +today.getDate()) { //如果是 今天发的
      result = "今天 " + DateUtil.dateToStr("HH:mm", date);
    } else if (date.getMonth() + "-" + date.getDate() == today.getMonth() + "-" +(today.getDate() - 1)) {
      //如果是 昨天发的
      result = "昨天 " + DateUtil.dateToStr("HH:mm", date);
    } else if (date.getMonth() + "-" + date.getDate() == today.getMonth() + "-" + (today.getDate() - 2)) {
      //如果是 前天发的
      result = "前天 " + DateUtil.dateToStr("HH:mm", date);
    } else {
      result = DateUtil.dateToStr("MM/dd HH:mm", date);
    }
  }
  return result;
});






